<?php

include_once("../dao/conexao_class.php");
include_once("../model/departamento_model_class.php");

class DepartamentoDAO {

    private $insert = "INSERT INTO departamento(dep_descricao) values(?);";
    private $update = "UPDATE departamento SET dep_descricao = ? WHERE dep_id = ?;";
    private $delete = "DELETE FROM departamento WHERE dep_id = ?;";
    private $select = "SELECT * FROM departamento;";
    private $select_by_id = "SELECT * FROM departamento from dep_id = ?;";
    private $select_departamento = "SELECT * FROM departamento WHERE dep_descricao = ?;";

    function insert($objDepartamento) {
        //conectando ao banco
        $objConn = new Conexao();
        $conn = $objConn->conecta();
//        var_dump($conn);
        //criando o preparedStatment
        if ($stm = $conn->prepare($this->insert)) {

            $stm->bind_param("s", $objDepartamento->getDescricao());

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        } else {
            return false;
        }
    }

    function update($objDepartamento) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->update)) {

            $stm->bind_param("si", $objDepartamento->getDescricao(), $objDepartamento->getId());
//            $stm->bind_param("i",);

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }

    function delete($id) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->delete)) {

            $stm->bind_param("i", $id);

            if ($stm->execute()) {
                $conn->close();
                return true;
            } else {
                $conn->close();
                return false;
            }
        }
        return false;
    }

    function findAllDepartamento() {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $depM = new DepartamentoModel();
        $lista = array();
        if ($stm = $conn->prepare($this->select)) {

            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $descricao);


            //para cada resultado encontrado
            $i = 0;
            while ($stm->fetch()) {
                $depM = new DepartamentoModel();
                
                $depM->setId($id);
                $depM->setDescricao($descricao);
                
                $lista[$i++] = $depM;
            }

            $conn->close();
            return $lista;
        }
    }

    function findDepartamentoById($id) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();
        $depM = new DepartamentoModel();
        $lista = array();
        if ($stm = $conn->prepare($this->select_by_id)) {
            
            $stm->bind_param('i', $id);
            
            $stm->execute();

            //atribui o resultado encontrado as variaveis
            $stm->bind_result($id, $descricao);

            //para cada resultado encontrado
            $i = 0;
            while ($stm->fetch()) {
                $depM = new DepartamentoModel();
                
                $depM->setId($id);
                $depM->setDescricao($descricao);
                
                $lista[$i++] = $depM;
            }

            $conn->close();
            return $lista;
        }
    }
    
    function existe($departamento) {

        $objConn = new Conexao();
        $conn = $objConn->conecta();

        if ($stm = $conn->prepare($this->select_departamento)) {
            
            $stm->bind_param("s", $departamento);
            
            $stm->execute();

            while ($stm->fetch()) {
                $conn->close();
                return true;
            }

        }
        
        $conn->close();
            return false;
    }
    
}

//$teste = new DepartamentoDAO();
//$dep = new DepartamentoModel();
////$dep->setId(3);
//$dep->setDescricao("Administraçao");
//
////$dep = 3;
//if($teste->insert($dep)){
//    printf("Deletado com sucesso!");
//}else{
//    printf("Erro ao deletar!");
//}
//
//var_dump($teste->findAllDepartamento());

